<template>
  <div class="talk">
    <button @click="getLoveTalk">获取一句土味情话</button>
    <ul>
      <li v-for="item in talkList" :key="item.id">
        {{ item.title }}
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts" name="LoveTalk">
// import { reactive } from "vue";
// import { nanoid } from "nanoid";
import { useTalkStore } from "../store/loveTalk";
// import axios from "axios";
import { storeToRefs } from "pinia";

const talkStore = useTalkStore();
// 这个pinia中的storeToRefs是将你state中的数据变成响应式，方便你解构。不会影响到actions中的方法
const { talkList } = storeToRefs(talkStore);

talkStore.$subscribe((mutate, state) => {
  console.log("talkStore里面保存的数据发生了变化", mutate, state);
  localStorage.setItem("talkList", JSON.stringify(state.talkList));
});
// 方法
async function getLoveTalk() {
  // 普通写法
  /* let {
    data: { content: title },
  } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");

  let obj = {
    id: nanoid(),
    title,
  };
  talkStore.talkList.push(obj);*/

  talkStore.getTalk();
}
</script>

<style scoped>
.talk {
  background-color: orange;
  padding: 10px;
  border-radius: 20px;
}
</style>
